/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare",
    "scprit/followup/app/datatable/datatable.servers.js",
    "scprit/followup/app/datatable/datatable.ui.js",],function($,$bb,$datatableServers,$datatableUi){
	var servers = {};
	var URL = $bb.baseUrl("followup");
    /**
     * 入参
     */
    var inParams = function(args){
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "patientName": $.trim($('#searchByName').val()),
                "hisNo": $.trim($('#searchByHisNo').val()),
                "phoneNum": $.trim($('#searchByPhone').val()),
                "sampleCode":"bioSample"
            }
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    }
    /**
     * columns
     */
    var type=function(val){
        return  val == "0" ? "尿液":val == "1" ? "血液":val == "2" ? "唾液":val == "3" ? "头发" : "-";
    }
    var columnsData = function(){
        var columnsArr=[
            {
                data: function(row,type){
                    return    row.hisNo1 ? row.hisNo1 : row.hisNo2 ? row.hisNo2 : row.hisNo3 ? row.hisNo3 : "-";
                }
            },
            {
                data: 'patientName'
            },
            {
                data: "age"
            },
            {
                data:"phoneNum"
            },
            {
                data: function(row,type){
                    return  row.sampleType == "0" ? "尿液":row.sampleType == "1" ? "血液":row.sampleType == "2" ? "唾液":row.sampleType == "3" ? "头发" : "-";
                }
            },
            {
                data: function(row, type, set, meta){
                    return row.sampleValue1 == "1" ? "已收集":"未收集";
                },
                className: "1"
            },
            {
                data: function(row, type, set, meta){
                    return row.sampleValue2 == "1" ? "已收集":"未收集";
                },
                className: "2"
            },
            {
                data: function(row, type, set, meta){
                    return row.sampleValue3 == "1" ? "已收集":"未收集";
                },
                className: "3"
            }
        ]
        return columnsArr;
    }
    /**
     * 样本收集
     */
    var sampleCollectionFun = function(parame,table){
        $bb.ajax({
            url: URL + "/followup/service/patient/patientSampleDataModify",
            data: JSON.stringify({
                data: parame
            }),
            success: function (data) {
                if(data.success){
                    table.api().draw(false);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "样本收集操作失败！");
                }
            }
        });
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        $('td[class]', row).click(function(){
            var className= $.trim($(this).attr("class"));
            var sessinonUserName = JSON.parse(sessionStorage.account).alias;
            var sampleType=data.sampleType;
            var sampleValueType="sampleValue"+className;
            var sampleFlag;
            if(data[sampleValueType]=="1"){
                sampleFlag = "0";
            }else{
                sampleFlag = "1";
            }
            var sampleFlagText = sampleFlag == "1" ? "已收集" : "未收集";
            var parame = {
                "oprInfo": {
                    "opCode": "1014",
                    "opNote": "【"+sessinonUserName + "】医生修改【" + data.patientName + "】患者的第【"+className+ "】次【"+type(sampleType)+ "】样本状态为" + sampleFlagText,
                    "opType": "2"
                },
                "sampleInfo": {
                    "followQueueId": data.followQueueId,
                    "patientId": data.patientId,
                    "patientSchemeId": data.patientSchemeId,
                    'sampleCode':'bioSample',
                    "sampleFlag": sampleFlag,
                    'sampleType':sampleType,
                    'sampleValue':Number(className)
                }
            };
            sampleCollectionFun(parame,$datatableUi.table);
        });
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        $("#dataCenter").prev("div.row").children().first().removeClass("col-xs-6").addClass("col-xs-12");
        $("#dataCenter").prev("div.row").children().last().remove();
        var h = '<div id="search" class="search">'+
                '<label>科研编号：'+
                '    <input id="searchByHisNo" type="text" class="form-control input-sm"></input>'+
                '</label>'+
                '<label>姓名：'+
                '    <input id="searchByName" type="text" class="form-control input-sm"></input>'+
                '</label>'+
                '<label>手机号：'+
                '    <input id="searchByPhone" type="text" class="form-control input-sm"></input>'+
                '</label>'+
                '</div>';
        $("#dataCenter").prev("div.row").children().first().append(h);
        $("#searchByName, #searchByHisNo, #searchByPhone").bind("input propertychange",function(){
            $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatableUi.table.api().ajax.params();   //获取dt请求参数
        });  
        $("#dataCenter_length select").change(function(){
            var length = $(this).val();
            $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatableUi.table.api().ajax.params();   //获取dt请求参数
            args = $.extend(args,{"length":length});    // 每页length个
        });
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "/followup/service/patientFollow/qryPtSampleList?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete
        };
        return options;
    }

	return servers;
});